﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace CommHelper
{
    public enum PostType
    {
        form,
        json
    }

    public class HelpRequest
    {
        private Encoding encoding = Encoding.UTF8;
        private String ContentType;
        public Int32 Timeout = 120000;

        public HelpRequest()
        {
            this.ContentType = "application/x-www-form-urlencoded";
        }

        public HelpRequest(PostType type)
        {
            if(type == PostType.form)
            {
                this.ContentType = "application/x-www-form-urlencoded";
            }
            else
            {
                this.ContentType = "application/json";
            }
        }

        public String get(String url, Dictionary<String, String> header)
        {
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.Timeout = Timeout;
            httpRequest.Method = "GET";
            foreach (KeyValuePair<string, String> kv in header)
            {
                httpRequest.Headers.Add(kv.Key, kv.Value);
            } 
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), encoding);
            string result = sr.ReadToEnd(); 
            int status = (int)httpResponse.StatusCode;
            sr.Close();
            return result;
        }

        public String get(String url)
        {
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.Timeout = Timeout;
            httpRequest.Method = "GET";
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), encoding);
            string result = sr.ReadToEnd(); 
            int status = (int)httpResponse.StatusCode;
            sr.Close();
            return result;
        }
         

        
        /// <summary>
        /// 返回JSon数据
        /// </summary>
        /// <param name="JSONData">要处理的JSON数据</param>
        /// <param name="Url">要提交的URL</param>
        /// <returns>返回的JSON处理字符串</returns>
        public string post(string Url,string Data)
        {
            return post(Url, Data, null);
        }

        public string post(string Url, string Data,Dictionary<String,String> Headers)
        { 
            string result = "";
            byte[] data = Encoding.UTF8.GetBytes(Data);
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url); 
            req.Method = "POST";
            req.Timeout = Timeout;
            //req.Timeout = 800;//设置请求超时时间，单位为毫秒 
            req.ContentType = this.ContentType;  
            req.ContentLength = data.Length;
            if (Headers != null)
            {
                foreach (KeyValuePair<String, String> kv in Headers)
                {
                    req.Headers.Add(kv.Key, kv.Value);
                }
            }
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length); 
                reqStream.Close();
            } 
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
            Stream stream = resp.GetResponseStream(); 
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            } 
            return result; 
        }
         
        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        { 
            //直接确认，否则打不开     
            return true;

        }
    }
}